<?php namespace Admin\Model;
use Hdphp\Model\Model;
class Goods extends Model{
	//数据表
	protected $table = "shop_goods";
	//完整表名
	protected $full = false;
	//自动验证
	protected $validate=array(
		//字段名: 表单字段名
		//验证方法: 函数或模型方法
		//验证条件: 1有字段时验证(默认)	2值不为空时验证  	3必须验证
		//验证时间: 1 插入时验证		2更新时空时验证 	3全部情况验证 (默认)
		//array('字段名','验证方法','提示信息',验证条件,验证时间),
		array('goods_name','required','商品名称不能为空',3,3),
		);
	//自动完成
	protected $auto=array(
		//字段名: 表单字段名
		//处理方法: 函数或模型方法
		//方法类型: string(字符串 默认)  function(函数)  method(模型方法)
		//验证条件: 1有字段时处理(默认)	2值不为空时 3必须处理
		//处理时间: 1 插入时  2更新时 3全部情况 (默认)
		//array('字段名','处理方法','方法类型',验证条件,验证时间),
		array('create_at','time','function',3,1),
		array('admin_id','getAdminId','method',3,1),
		array('thumb_pic','getThumbPic','method',3,3),
		array('list_pic','getListPic','method',3,3),
		);
	protected function getAdminId()
	{
		return $_SESSION['admin_id'];
	}
    //自动完成，获取商品的列表页图片
	protected function getListPic()
	{
		if($file = Upload::make('list_pic'))
		{
			return $file[0]['path'];
		}
		return Q('post.list_pic','');
	}
    //自动完成，获取商品的缩略图片
	protected function getThumbPic()
	{
		if($file = Upload::make('thumb_pic'))
		{
			return $file[0]['path'];
		}
		return Q('post.thumb_pic','');
	}
	public function getAll($cat_id=''){
		if($cat_id)
		{
			//读取指定栏目的商品
			$page = Page::make($this->where("cat_id=$cat_id and is_delete=0")->count());
			$data = Db::table('shop_goods g')->where("g.cat_id=$cat_id and is_delete=0")
			->limit(Page::limit())
			->join('shop_cat c','g.cat_id','=','c.cat_id')
			->join('shop_type t','g.type_id','=','t.id')
			->get();
		}
		else
		{
			//读取所有商品
			$page = Page::make($this->where("is_delete=0")->count());
			$data = Db::table('shop_goods g')
			->limit(Page::limit())
			->where("is_delete=0")
			->join('shop_cat c','g.cat_id','=','c.cat_id')
			->join('shop_type t','g.type_id','=','t.id')
			->get();
		}
		return array('data'=>$data,'page'=>$page);
	}



	public function getDelete()
	{
		$page = Page::make($this->where("is_delete=1")->count());
		$data = Db::table('shop_goods g')
		->limit(Page::limit())
		->where("is_delete=1")
		->join('shop_cat c','g.cat_id','=','c.cat_id')
		->join('shop_type t','g.type_id','=','t.id')
		->get();

		return array('data'=>$data,'page'=>$page);
	}



	public function store()
	{
		if($this->create())
		{
			if ($goods_id = $this->add()) {
				$pics = new \Admin\Model\Pics;
				$pics->store($goods_id);
				$goodsAttr = new \Admin\Model\GoodsAttr;
				$goodsAttr->addGoodsAttr($goods_id);
			}
			return true;
		}
	}
	//修改商品
	public function edit()
	{

		if($this->create())
		{
			// p($_POST);exit;
			if($this->save())
			{
				$pics = new \Admin\Model\Pics;
				$pics->store($_POST['goods_id']);
				$goodsAttr = new \Admin\Model\GoodsAttr;
				$goodsAttr->addGoodsAttr($_POST['goods_id']);
			}
			return true;
		}
	}


	public function is_delete()
	{
		$data = Db::table('shop_goods')->where('goods_id',Q('goods_id'))->pluck('is_delete');
		$is_delete = $data==1?0:1;
		$re = Db::table('shop_goods')->where('goods_id',Q('goods_id'))->update(array('is_delete'=>$is_delete));
		return $re;
	}
	//读取商品数据
	public function one($goods_id)
	{
		return $this->find($goods_id);
	}




	public function getAttrList($goods_id,$type_id)
	{
		$html='';
		if($goods_id){
			$attrs = Db::table('shop_attr sa')
			->leftJoin('shop_goods_attr ga','ga.attr_id','=','sa.attr_id')
			->where('sa.shop_type_id',$type_id)
			->where('ga.goods_id',$goods_id)
			->orderBy('orderby','ASC')
			->get();
		// 	$sql = "SELECT a.attr_id,a.attr_name,a.attr_type,a.show_type,a.attr_values,a.shop_type_id,ga.attr_value,ga.add_price,ga.goods_id
		// FROM hd_shop_attr a
		// LEFT JOIN
		// ((SELECT * FROM hd_shop_goods_attr WHERE goods_id=$goods_id) AS ga)
		// ON a.attr_id =ga.attr_id
		// WHERE a.shop_type_id=$type_id";
		// $attrs = Db::select($sql);
		// p($attrs);
			foreach ($attrs as $a) {

				switch ($a['show_type']) {
					case 1:
					// $priceField='<input type="hidden" name="add_price[]" value="0">';
					$priceField='加价:
					<input type="text" name="add_price[]" value="0"><span class="addPrice hd-btn hd-btn-success" onclick = "addPrice(this)">[+]</span>';
					// 文本框
					$field =<<<php
					<tr>
						<th>{$a['attr_name']}</th>
						<td>
							<input type="hidden" name="good_attr_id[]" value="{$a['good_attr_id']}">
							<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
							<input type="text" name="attr_values[]" value="{$a['attr_values']}" style="text-align:center;">
						</td>
						<td>$priceField</td>
					</tr>
					<br/>
php;
					break;
					case 2:
					$priceField='加价:
					<input type="text" name="add_price[]" value="'.$a["add_price"].'"><span class="addPrice hd-btn hd-btn-success" onclick = "addPrice(this)">[+]</span>';
					// 列表框
					$attrValue = preg_split('@\n@',$a['attr_value']);
					$options="";
					foreach($attrValue as $opt)
					{
						if (trim($a['attr_values'])==trim($opt)) {
							$option='<option selected value="'.$opt.'">'.$opt.'</option>';
						}
						else{
							$option='<option value="'.$opt.'">'.$opt.'</option>';
						}
						$options.=$option;
					}
					$field=<<<php
					<tr>
						<th>{$a['attr_name']}</th>
						<td>
							<input type="hidden" name="good_attr_id[]" value="{$a['good_attr_id']}">
							<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
							<select name="attr_values[]" style="width:155px;text-align:center;">
								{$options}
							</select>
						</td>
						<td>$priceField</td>
					</tr>
					<br/>
php;
					break;
				}
				$html.=$field;
			}
		}
		else
		{
			$attrs = Db::table('shop_attr sa')
			->where('shop_type_id',$type_id)
			->orderBy('orderby','ASC')
			->groupBy('sa.attr_id')
			->get();
			foreach ($attrs as $a) {

				switch ($a['show_type']) {
					case 1:
					// 文本框
					// $priceField='<input type="hidden" name="add_price[]" value="0">';
					$priceField='加价:
					<input type="text" name="add_price[]" value="0"><span class="addPrice hd-btn hd-btn-success" onclick = "addPrice(this)">[+]</span>';
					$field =<<<php
					<tr>
						<th>{$a['attr_name']}</th>
						<td>
							<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
							<input type="text" name="attr_values[]" value="" style="text-align:center;">
						</td>
						<td>$priceField</td>
					</tr>
					<br/>
php;
					break;
					case 2:
					// 列表框
					$priceField='加价:
					<input type="text" name="add_price[]" value="0"><span class="addPrice hd-btn hd-btn-success" onclick = "addPrice(this)">[+]</span>';
					$attrValue = preg_split('@\n@',$a['attr_value']);
					$options="";
					foreach($attrValue as $opt)
					{
						$options.='<option value="'.$opt.'">'.$opt.'</option>';
					}
					$field=<<<php
					<tr>
						<th>{$a['attr_name']}</th>
						<td>
							<input type="hidden" name="attr_id[]" value="{$a['attr_id']}">
							<select name="attr_values[]" style="width:155px;text-align:center;">
								{$options}
							</select>
						</td>
						<td>$priceField</td>
					</tr>
					<br/>
php;
					break;
				}
				$html.=$field;
			}
		}
		return $html;
	}
}
